import Observe from "./Observe.js";
import Compile from "./Compile.js";

export default class Vue {
  constructor(options) {
    this.$el = options.el;
    this.$data = options.data || {};
    this.$methods = options.methods || {};
    this.$watchs = options.watch || {};
    this.$options = options;
    this.computed = options.computed || {};

    new Observe(this, this.$data);
    this.proxyData(this.$data);

    this.$options.beforeCreate.call(this);
    new Compile(this, this.$el);
    this.$options.mounted.call(this);
  }

  //获取数据优化使用计算属性
  getValue(name) {
    const computed = this.computed[name];
    return computed ? computed.call(this) : this.$data[name];
  }

  //设置代理 拦截 Vue实例的属性访问
  proxyData(data) {
    for (let [key, value] of Object.entries(data)) {
      Object.defineProperty(this, key, {
        get() {
          return data[key];
        },
        set(newValue) {
          data[key] = newValue;
        },
      });
    }
  }
}
